/**
 * Created by xiewl on 2016/5/9.
 */
//获取随访模板列表
var getTempList = function (index) {
    index = index || 1;
    var pageSize = 10;
    var urlParamObj = $$.util.urlParamToObj();
    new followupPlan().getFollowupTempList(index,pageSize,urlParamObj['subdepttype'],urlParamObj['hospitalid'],function (res,status) {
        res = JSON.parse(res);
        if(res.code == '000'){
            var list = JSON.parse(res.data);
            var html = '';
            for(var i = 0,len = list.length; i < len ; i++){
                var deptName = '';
                if(list[i].docdept && list[i].docdept.subDeptTypeName) deptName = list[i].docdept.subDeptTypeName;
                var timeStamp = '1970-01-01';
                if(list[i].modifydate) timeStamp = $$.util.timeStampToDay(list[i].modifydate);
                html +=[
                    '<tr>',
                    '<td>'+ ((i+1) + (index - 1)*pageSize) +'</td>',
                    '<td>'+ list[i].name +'</td>',
                    '<td>'+ list[i].doctorusername +'</td>',
                    '<td>'+ deptName +'</td>',
                    '<td>'+ list[i].usagecount +'</td>',
                    '<td>'+ timeStamp +'</td>',
                    '<td data-id="'+ list[i].id +'"><a href="./edit-base.html?templateid='+ list[i].id +'" title="编辑"><i class="material-icons"></i></a><a href="javascript:;" class="delete" title="删除"><i class="delete-icon"></i></a></td>',
                    '</tr>'
                ].join('');
            }
            $('.table tbody').append(html);
            $('.outer').addClass('active');
            $('html,body').animate({scrollTop : $(window).scrollTop() + 500},500);
            if(len < pageSize){
                $('.check-more').addClass('disabled').attr('data-loading','true').text('没有更多了~');
            }else{
                $('.check-more').attr('data-loading','false').text('查看更多随访模板');
            }
        }
    });
    //加载更多随访模板
    $('.check-more').click(function () {
        var current = $(this);
        if(current.attr('data-loading') == 'true') return false;
        current.attr('data-loading','true').html('<i class="material-icons loading">&#xE5D5;</i>');
        getTempList(++index);
    });
};
//随访模板操作
var tempListOperate = function () {
    //删除模板
    $('table').on('click','.delete',function () {
        var dataId=$(this).parent().attr('data-id');
        var tbody=$(this).parents().eq(1);
        $$.modal.confirm('确认删除吗？','取消','确认',function(){
            new followupPlan().delFollowupTemp(dataId,function(res,status){
                res=JSON.parse(res);
                if(res.code==000){
                    $('#confirmModal').modal('hide');
                    tbody.remove();
                }
            })
        });
    });
};



$(function () {
    getTempList();
    tempListOperate();
});